/*********************************************************
          File Name:event.h
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Mon 27 Jul 2015 07:23:19 PM CST
**********************************************************/

#ifndef EVENT_H_
#define EVENT_H_

#include <pthread.h>

#ifdef __linux__
#include <sys/epoll.h>
#elif (defined __FreeBSD__) || (defined __APPLE__)
#include <sys/types.h>
#include <sys/event.h>
#else
#error "platform not support!"
#endif

#define MAX_EVENTS      309               // size of struct epoll_event *event

struct task {
        int sockfd;
        int exit; // exit: 0-continue, 1-exit;
        int done; // done: 0-busy; 1-idle
        int work;      // condtition
        pthread_t id;
        pthread_mutex_t lock;
        pthread_rwlock_t rw;
        pthread_cond_t cond;
};

typedef void (*cb)(struct task *, int);

#ifdef __linux__

typedef struct epoll_event Event;

#elif (defined __FreeBSD__) || (defined __APPLE__)

typedef struct kevent Event;

#endif

int ev_init(int);

int ev_run(int *, int, int *, Event *, int, struct task *, cb);

void ev_done(int *, Event *);

int assign_job(int n, struct task *job);

#endif
